const CONTEXT = require('../../utils/public_var.js')
exports.sendTextMsg = function (that, app, url) {
  //TODO  Bug
  var that = that
  if (app.globalData.OpenID == "" || app.globalData.OpenID == undefined) {
    wx.showModal({
      title: '',
      content: '请您登录验证',
      confirmText: '登录',
      complete: (res) => {
        if (res.cancel) {}
        if (res.confirm) {
          wx.redirectTo({
            url: '/pages/login/atLogin/atLogin',
          })
        }
      }
    })
    return
  }
  if (that.data.totalCount > 0) {
    console.log("有" + that.data.totalCount + "使用次数")
    var tx = that.data.input_tx
    if (that.data.input_tx != "") {
      var message = {
        "role": "user",
        "content": that.data.input_tx,
        "msgtype": CONTEXT.TEXT
      }
      var list = that.data.MessageList
      list.push(message)
      that.setData({
        MessageList: list,
      })
      that.setData({
        intoViewName: 'endview'
      })
      wx.showLoading({
        title: "主公请稍后，我在构思中"
      })
      wx.request({
        url: url,
        header: {
          'content-type': 'application/json',
        },
        method: 'POST',
        data: {
          "prompt": getPromoteList(that.data.MessageList),
          'openid': app.globalData.OpenID
        },
        success(res) {
          wx.hideLoading()
          console.log(res.data)
          if (res.data.statusCode == 200) {
            var sys_message = {
              "role": "system",
              "content": res.data.completeOpenAnswer.gptAnswer,
              "msgtype": CONTEXT.TEXT

            }
            list.push(sys_message)
            that.setData({
              MessageList: list,
              input_tx: "",
              totalCount: res.data.completeOpenAnswer.remainingGPTCount,
            })
            app.setCount(res.data.completeOpenAnswer.remainingGPTCount)
            
              that.setData({
                intoViewName: 'endview'
              })
           
            
            // module.exports.saveTextMsg(list, app)
            app.saveMsg(that.data.bar_type,list)
            // const child = that.selectComponent('.bottom_class');
            // child.initInputTx();
            that.initSendUI()

           
          } else {

          }


        },
        fail(res) {
          wx.hideLoading()
          console.log(res)
        }
      })

    } else {

      wx.showToast({
        title: '请输入您的问题',
        icon: "none"
      })
    }
  } else {
    // 没有免费使用次数了
    wx.showModal({
      title: '体验用尽',
      content: '在下养家糊口，\n恳请主公雇佣在下',
      confirmText: "雇佣",
      cancelText: "取消",
      complete: (res) => {
        if (res.cancel) {}
        if (res.confirm) {
          wx.navigateTo({
            url: '/pages/sita/pay/paybill',
          })
        }
      }
    })
  }
}
exports.initMsgData = function (that, data, app) {
  console.log("app.chatList.textTypeList")
 
  var chatMsg =  app.getMsg(that.data.bar_type)
  console.log(chatMsg)
  if (chatMsg != null && chatMsg != undefined && chatMsg.length > 0) {
    that.setData({
      MessageList: chatMsg
    })
    that.setData({
      intoViewName: 'endview'
    })
  } else {

    var initData = [{
      "role": 'system',
      "content": data,
      "msgtype": CONTEXT.TEXT
    }]

    that.setData({
      MessageList: initData
    })
  }





}

function getPromoteList(list) {

  let slicedItems = null
  // if (list.length == 2) {
  //   slicedItems = list.slice(list.length - 1, list.length);
  // } else if (list.length == 3) {
  //   slicedItems = list.slice(list.length - 2);
  // } else {
  //   slicedItems = list.slice(list.length - 3);
  // }
  // 
  slicedItems = list.slice(list.length - 1, list.length);
  console.log(slicedItems)
  return slicedItems
}
exports.inputChangetx = function (that, app, e) {
  var that = that
  var text = e.detail.data
  that.setData({
    input_tx: text
  })
}

